<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Url;
use think\Session;
use think\Controller;

/**
 * 
 */
class Folder extends Base
{
	/*列表页面*/
	public function index()
	{
		if (Request::instance()->isPost()) {
			$name = request::instance()->post('name');
			$where['name'] = array('like',"%$name%");
		}
		$where['is_del'] = 0;
		$list = DB::name('person_folder')->where($where)->order('id desc')->paginate(15);
		$page = $list->render();
		$list = $list->all();
		return $this->fetch('index',[
			'list'		=>	$list,
			'page'		=>	$page,	
		]);
	}

	/*添加个人信息*/
	public function create()
	{
		if(Request::instance()->isPost())
		{
			$data = Request::instance()->post();
			$data = $this->get_arr($data);
		
			$data['family'] = serialize($data['family']);
			$data['education'] = serialize($data['jiaoyu']);
			$data['work'] = serialize($data['work']);
			$data['contact'] = serialize($data['contact']);
			$data['entry_id'] = Session::get('manage_id');
			$data['addtime'] = time();
			unset($data['jiaoyu']);
			$res = DB::name('person_folder')->insert($data);
			if($res)
			{
				$this->success('信息录入成功',url('index/index/index',array('con'=>'folder','act'=>'index')));
			}else{
				$this->error('信息录入失败');
			}
		}
		return $this->fetch();
	}
	/*修改个人信息*/
	public function update($id)
	{
		/*判断权限   修改个人档案权限 ： folder_update */
			$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'folder_update');
			if($auth !=true){//没有删除菜单的权限
				$this->error('权限不足');
				exit();				
			}
		if(Request::instance()->isPost())
		{
			$data = Request::instance()->post();
			$data = $this->get_arr($data);
			$data['family'] = serialize($data['family']);
			$data['education'] = serialize($data['jiaoyu']);
			$data['work'] = serialize($data['work']);
			$data['contact'] = serialize($data['contact']);
			unset($data['jiaoyu']);
			$res = DB::name('person_folder')->where('id = '.$id)->setField($data);
			if($res){
				$this->success('修改成功',url('index/index/index',array('con'=>'folder','act'=>'index')));
			}else{
				$this->error('修改失败');
			}
		}
		$data = DB::name('person_folder')->where('id = '.$id)->find();
		$family = unserialize($data['family']);
		$education = unserialize($data['education']);
		$work = unserialize($data['work']);
		$contact = unserialize($data['contact']);
		return $this->fetch('create',[
			'data'		=>	$data,
			'family'	=>	$family,
			'education'	=>	$education,
			'work'		=>	$work,
			'contact'	=>	$contact,
		]);
	}
	/*个人档案展示*/
	public function show($id)
	{
		$data = DB::name('person_folder')->where('id = '.$id)->find();
		$family = unserialize($data['family']);
		$education = unserialize($data['education']);
		$work = unserialize($data['work']);
		$contact = unserialize($data['contact']);
		return $this->fetch('show',[
			'data'		=>	$data,
			'family'	=>	$family,
			'education'	=>	$education,
			'work'		=>	$work,
			'contact'	=>	$contact,
		]);
	}
	/*删除个人档案 只修改状态不删除数据*/
	public function delofolder()
	{
		if(Request::instance()->isPost())
		{
			$id = Request::instance()->post('id');
			/*判断权限   删除个人档案权限 ： folder_del */
			$authmenu = new \think\Authmenu();
			$uid = Session::get('manage_id');
			$auth=$authmenu->check($uid,'folder_del');
			if($auth !=true){//没有删除菜单的权限
				$res['sta'] = '-1';
				$res['msg'] = '权限不足';
				
			}else{
				$del = DB::name('person_folder')->where('id = '.$id)->setField('is_del',1);
				if($del){
					$res['sta'] = '1';
					$res['msg'] = '删除成功';
				}else{
					$res['sta'] = '0';
					$res['msg'] = '删除失败';
				}
			}
			return json($res);
		}
	}

	/*修改状态  在职  和离职两种状态*/
	public function changeStatus()
	{
		if(Request::instance()->isPost())
		{
			$id = Request::instance()->post('id');
			$data['status'] = Request::instance()->post('status');
			$result = DB::name('person_folder')->where('id ='.$id)->setField($data);
			if($result){
				$res['sta'] = '1';
				$res['msg'] = '修改成功'; 
			}else{
				$res['sta'] = '0';
				$res['msg'] = '修改失败';
			}
			return json($res);
		}
	}
	/*去除数组中的空值*/
	public function get_arr($arr)
	{
	    foreach ($arr as $k=>$v)
	    {
	        if(is_array($arr[$k]))
	        {
	            $arr[$k] = $this->get_arr($arr[$k]);
	        }
	        else
	        {
	            if(empty($arr[$k]))
	            {
	                unset($arr[$k]);
	            }
	        }
	    }
    	return $arr;
	}
}


?>